﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TGIApp.Data.EntityFramework;
using TGIApp.Data;
using TGIApp.Model;

namespace TGIApp.Business
{
    public class ProviderHelper
    {
        private IRepository repository;
        public bool ValidateUser(string username, string password)
        {
            repository = new GenericRepository();
            var theUser= repository.GetAll<User>().Where(user => user.UserName == username && user.Password == password).FirstOrDefault();
            if (theUser != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public List< string> GetRolesForUser(string username)
        {
            repository = new GenericRepository();
            var theUser =repository.GetAll<User>().Where(user => user.UserName == username).FirstOrDefault();
            var roles = repository.GetAll<Role>().Where(role => role.Id == theUser.Role_Id).Select(role=> role.Name).ToList<string>();
            return roles;
        }

        public bool IsUserInRole(string username, string roleName)
        {
            repository = new GenericRepository();
            var theUser = repository.GetAll<User>().Where(user => user.UserName == username).FirstOrDefault();
            var roles= repository.GetAll<Role>().Where(role => role.Id == theUser.Role_Id && role.Name == roleName);
            if (roles.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
     
}
